﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MSExcel = Microsoft.Office.Interop.Excel;

namespace InfoPick.Lib
{
    public class MSCInfo
    {
        public static List<MSCInfo> Load(string file)
        {
            MSExcel.Application app = new MSExcel.Application();
            MSExcel.Worksheet sheet = null;

            app.Workbooks.Open(file);

            sheet = app.Worksheets.get_Item(1);//第一页,


            int maxCol = sheet.UsedRange.Columns.Count;
            int maxRow = sheet.UsedRange.Rows.Count;

            //MSCInfo[] mSCInfos = new MSCInfo[maxRow];
            List<MSCInfo> mSCInfoList = new List<MSCInfo>();

            for (int i = 2; i <= maxRow; i++)//下标从一开始
            {
                if (sheet.Cells == null)
                {
                    break;
                }
                else
                {
                    if (sheet.Cells[i, 1].value == null || sheet.Cells[i, 2].value == null || sheet.Cells[i, 3].value == null)
                    {
                        continue;
                    }
                }
                string name = sheet.Cells[i, 1].value.ToString();
                string gridname = sheet.Cells[i, 2].value.ToString();
                object o = sheet.Cells[i, 3];
                string mscid = sheet.Cells[i,3].value.ToString();
                MSCInfo tmpInfo = new MSCInfo();
                tmpInfo.Name = name;
                tmpInfo.GridName = gridname;
                tmpInfo.MSCID = mscid;
                mSCInfoList.Add(tmpInfo);             
            }
            return mSCInfoList;
        }

        public static List<MSCInfo> Loadtxt(string file)
        {
            List<MSCInfo> mSCInfoList = new List<MSCInfo>();
            
            StringMath math = new StringMath();
            bool start = false;
            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            var gb18030 = Encoding.GetEncoding("GB18030");

            StreamReader sr = new StreamReader(fs, gb18030);
             //使用StreamReader类来读取文件 
            while (!sr.EndOfStream)
            {
                string strReadLine = sr.ReadLine(); //读取每行数据      
                if (strReadLine.Contains("LAIGCI说明"))
                {
                    start = true;
                }
                if (strReadLine.Contains("结果个数"))
                {
                    start = false;
                }
                if (start)
                {
                    if (strReadLine.Contains("CQGS") && strReadLine.Contains("86"))
                    {
                        string[] xx = System.Text.RegularExpressions.Regex.Split(strReadLine, @"\s+");
                        int countX = xx.Length;
                        string mscid = xx[2];
                        //string tmpStr = math.FindstrBackSignInclue(strReadLine, "C");
                        //string gridname = math.FindstrBetweenInclude(tmpStr, "C", " ");
                        //string name = math.FindstrBackSign(tmpStr, " ");
                        string gridname = xx[countX - 3];
                        string name = xx[countX - 2];
                        if (mscid != null && gridname != null && name != null)
                        {
                            MSCInfo tmpSCInfo = new MSCInfo();
                            tmpSCInfo.MSCID = mscid;
                            tmpSCInfo.GridName = gridname;
                            tmpSCInfo.Name = name;
                            mSCInfoList.Add(tmpSCInfo);
                        }
                    }
                }
            }
            return mSCInfoList;
        }

        /// <summary>
        /// msc id
        /// </summary>
        public string MSCID { get; set; }
        /// <summary>
        /// 片区
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 网元名称
        /// </summary>
        public string GridName { get; set; }
        
    }
}
